package com.magicborrow.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.activity.BorrowStuffDetailActivity;
import com.magicborrow.beans.CommentListBean;
import com.magicborrow.huanxin.ChatActivity;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.ViewPressedUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;

/**
 * Created by ghost on 16/3/28.
 */
public class StuffCommentsRecordAdapter extends RefreshAdapter<CommentListBean.DataEntity.CommentBean> {

    public StuffCommentsRecordAdapter(ArrayList<CommentListBean.DataEntity.CommentBean> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateItem(ViewGroup parent, int viewType) {
        return new CommentsViewHolder(LayoutInflater.from(context).inflate(R.layout.stuff_comments_record_item, null));
    }

    @Override
    public void onBindItem(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof CommentsViewHolder) {
            CommentsViewHolder hol = (CommentsViewHolder) holder;
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, BorrowStuffDetailActivity.class);
                    //intent.putExtra("bean",data.get(position));
                    //intent.putExtra("bean",data.get(position));
                    intent.putExtra("canEdit", true);
                    context.startActivity(intent);
                }
            });
            hol.tvName.setText(data.get(position).getAuthor().getNickname());
            hol.rbPingfen.setRating(data.get(position).getAvgStar());
            hol.imvMsg.setOnTouchListener(ViewPressedUtil.TouchPress);
            hol.imvMsg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!UserTools.isLogin(context, true))
                        return;
                    //TODO implement
                    context.startActivity(new Intent(context, ChatActivity.class).putExtra("chatType", 1)
                            .putExtra("userId", data.get(position).getAuthor().getId() + "")
                            .putExtra("username", data.get(position).getAuthor().getNickname()).putExtra("avatarPath",
                                    data.get(position).getAuthor().getAvatar80()));
                }
            });
            hol.tvRecord.setText(data.get(position).getContent());
            DisplayImageOptions options = new DisplayImageOptions.Builder().displayer(new RoundedBitmapDisplayer(context.getResources().getDimensionPixelOffset(R.dimen._30dp))).build();

            ImageLoader.getInstance().displayImage(Constants.ADDRESS + data.get(position).getAuthor().getAvatar80(),
                    hol.imvHead, options);
        }

    }

    public class CommentsViewHolder extends RecyclerView.ViewHolder {
        private ImageView imvHead;
        private TextView tvName;
        private RatingBar rbPingfen;
        private ImageView imvMsg;
        private TextView tvRecord;

        public CommentsViewHolder(View view) {
            super(view);
            imvHead = (ImageView) view.findViewById(R.id.imv_head);
            tvName = (TextView) view.findViewById(R.id.tv_name);
            rbPingfen = (RatingBar) view.findViewById(R.id.rb_pingfen);
            imvMsg = (ImageView) view.findViewById(R.id.imv_msg);
            tvRecord = (TextView) view.findViewById(R.id.tv_record);
        }
    }

}
